﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

/// <summary>
/// 对字典的扩展
/// </summary>
public static class DictionaryExtend
{
    /// <summary>
    /// 批量添加 Key，值为该类型的默认值
    /// </summary>
    /// <typeparam name="TKey"></typeparam>
    /// <typeparam name="TValue"></typeparam>
    /// <param name="dic"></param>
    /// <param name="keyArr"></param>
    /// <param name="isOverride">如果这个键已存在，是否覆盖原来的值</param>
    /// <returns></returns>
    public static Dictionary<TKey, TValue> AddRangeWithDefaultValue<TKey, TValue>(this Dictionary<TKey, TValue> dic, IEnumerable<TKey> keyArr, bool isOverride)
    {
        foreach (var item in keyArr)
        {
            try
            {
                dic.Add(item, default);
            }
            catch (ArgumentException)
            {
                if (isOverride) dic[item] = default;
                else continue;
            }
        }

        return dic;
    }

    /// <summary>
    /// 遍历字典，为所有键值对的值赋初值
    /// </summary>
    /// <typeparam name="TKey"></typeparam>
    /// <typeparam name="TValue"></typeparam>
    /// <param name="dic"></param>
    /// <param name="value">初始值</param>
    /// <param name="where">可以只为某些键对应的值赋值</param>
    /// <returns></returns>
    public static Dictionary<TKey, TValue> ForeachAndResetDefaultValue<TKey, TValue>(this Dictionary<TKey, TValue> dic, TValue value = default, Func<TKey, bool> where = null)
    {
        //List<TKey> list = new List<TKey>(dic.Keys);
        //foreach (var item in list)
        //{
        //    if(where == null || where != null && where(item))
        //    {
        //        dic[item] = value;
        //    }
        //}

        //return dic;
        return ForeachAndModifyValue(dic, item => value, where);
    }
    /// <summary>
    /// 可以遍历并对字典中的值进行操作，可修改
    /// </summary>
    /// <typeparam name="TKey"></typeparam>
    /// <typeparam name="TValue"></typeparam>
    /// <param name="dic"></param>
    /// <param name="callback">对字典中的某一个键值对的值的操作。对值类型也有效</param>
    /// <param name="where">可以只为某些键对应的值赋值</param>
    /// <returns></returns>
    public static Dictionary<TKey, TValue> ForeachAndModifyValue<TKey, TValue>(this Dictionary<TKey, TValue> dic, Func<TValue, TValue> callback, Func<TKey, bool> where = null)
    {
        List<TKey> list = new List<TKey>(dic.Keys);
        foreach (var item in list)
        {
            if (where == null || where != null && where(item))
            {
                if (callback != null)
                    dic[item] = callback.Invoke(dic[item]);
            }
        }

        return dic;
    }

    /// <summary>
    /// 设置某键对应的值。如果键不存在，则创建
    /// </summary>
    public static void SetValueByKey<TKey, TValue>(this Dictionary<TKey, TValue> dic,TKey key,TValue val)
    {
        if(!dic.ContainsKey(key))
        {
            dic.Add(key, val);
        }
        else
        {
            dic[key] = val;
        }

    }
}

